package com.boco.blch.analysiser.compliance.HW.manager;

import com.boco.blch.analysiser.compliance.ComplianceResolved;
import com.boco.blch.analysiser.util.XmlUtil;

/**
 * HDFS用户访问权限控制安全基线要求项   【控制节点】
 * 【控制节点】
 * 1、找到配置文件 hdfs-site.xml的配置项
 * dfs.permissions.enabled的值为true；
 * fs.permissions.umask-mode 的值为 077
 * 2、/opt/huawei/bigdata/etc/2_20_NameNode/core-site.xml的配置项hadoop.security.authorization 的值 为 true;
 * 
 * @author admin
 *
 */
public class BOCO_Hdfs_VisitCtl_UserVisitCtl implements ComplianceResolved {
	
	private final static String CORE_NAME = "hadoop.security.authorization";
	private final static String HDFS_ENABLED = "dfs.permissions.enabled";
	private final static String HDFS_MODE = "fs.permissions.umask-mode";

	@Override
	public int realEchoResolved(String echoResult) {
		int result = FAILED;
		if(echoResult != null && echoResult.length() > 0){
			if(echoResult.indexOf("NameNode-start") != -1){//控制节点
				String strResult = echoResult.substring(echoResult.indexOf("NameNode-start")+"NameNode-start".length(), echoResult.indexOf("NameNode-end")).trim();
				result = getResult(strResult);
			}
			if(result == FAILED && echoResult.indexOf("JournalNode-start") != -1){//控制节点
				String strResult = echoResult.substring(echoResult.indexOf("JournalNode-start")+"JournalNode-start".length(), echoResult.indexOf("JournalNode-end")).trim();
				result = getResult(strResult);
			}
		}
		return result;
	}
	
	/**
	 * 【控制节点】
	 * 1、找到配置文件 hdfs-site.xml的配置项
	 * dfs.permissions.enabled的值为true；
	 * fs.permissions.umask-mode 的值为 077
	 * 2、/opt/huawei/bigdata/etc/2_20_NameNode/core-site.xml的配置项hadoop.security.authorization 的值 为 true;
	 * @param echoResult
	 * @return
	 */
	private int getResult(String echoResult){
		int result = FAILED;
		if(echoResult != null && echoResult.indexOf("core-site-start") != -1){//yarn-site.xml解析
			String coreXml = echoResult.substring(echoResult.indexOf("core-site-start")+"core-site-start".length(), echoResult.indexOf("core-site-end"));
			//System.out.println("coreXml---:"+coreXml);
			if(coreXml != null && coreXml.length() > 0){
				String value = XmlUtil.getValueByName(coreXml, CORE_NAME).trim();
				//System.out.println("value---:"+value);
				if(value != null && value.equals("true")){
					//System.out.println("1111");
					result = SUCCESS;
				}else{
					result = FAILED;
				}
			}
		}
		if(result == SUCCESS){
			//System.out.println("2222");
			//hdfs-site.xml，
			String hdfsXml = echoResult.substring(echoResult.indexOf("hdfs-site-start")+"hdfs-site-start".length(), echoResult.indexOf("hdfs-site-end"));
			if(hdfsXml != null && hdfsXml.length() > 0){
				String enabledValue = XmlUtil.getValueByName(hdfsXml, HDFS_ENABLED).trim();//QueueA,QueueB
				//System.out.println("value------"+value);
				//dfs.permissions.enabled的值为true；
				if(enabledValue != null && enabledValue.length() > 0 && enabledValue.equals("true")){
					result = SUCCESS;
					//fs.permissions.umask-mode 的值为 077
					String modeValue = XmlUtil.getValueByName(hdfsXml, HDFS_MODE).trim();
					if(modeValue != null && modeValue.length() > 0 && modeValue.equals("077")){
						result = SUCCESS;
					}else{
						result = FAILED;
					}
				}else{//hdfs-site.xml相关配置项没有值
					result = FAILED;
				}
			}
		}
		return result;
	}
	
	


}
